package com.example.teamtribe.common.model;

import android.os.Bundle;
import android.util.Log;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.database.User;
import com.example.teamtribe.json.JsonIds;
import com.example.teamtribe.json.JsonMessage;
import com.example.teamtribe.json.JsonRequest;
import com.example.teamtribe.json.JsonUser;
import com.example.teamtribe.utils.HttpUtil;
import com.example.teamtribe.utils.URLStore;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class UserModel {

    private static UserModel instance;
    private UserModel() {}
    public static UserModel getInstance() {
        if (instance == null) {
            synchronized (UserModel.class) {
                if (instance == null) {
                    instance = new UserModel();
                }
            }
        }
        return instance;
    }

    public void getHead(final String fileName, final FileModel.FileBack back) {
        FileModel.getInstance().download(fileName, 0, back);
    }

    public void getUserNoHead(String token, int id, final Back back) {
        Map<String, String> params = new HashMap<>();
        params.put("id", id + "");
        params.put("TeamTribeToken", token);
        HttpUtil.post(URLStore.USERINFO_GET, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                JsonUser jsonUser = new Gson().fromJson(json, JsonUser.class);
                Log.d("user", json);

                final int status = jsonUser.getCode();
                if (status != 0) {
                    back.failure(HttpUtil.getCodeMeg(status));
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putParcelable("user", jsonUser.getUser());
                    back.success(bundle);
                }
            }
        });
    }

    public void getUser(String token, int id, final FileModel.FileBack back) {
        Map<String, String> params = new HashMap<>();
        params.put("id", id + "");
        params.put("TeamTribeToken", token);
        HttpUtil.post(URLStore.USERINFO_GET, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                JsonUser jsonUser = new Gson().fromJson(json, JsonUser.class);
                Log.d("user", json);

                final int status = jsonUser.getCode();
                if (status != 0) {
                    back.failure(HttpUtil.getCodeMeg(status));
                } else {
                    final User user = jsonUser.getUser();
                    Bundle b = new Bundle();
                    b.putParcelable("user", user);
                    back.success(b);

                    getHead(user.getPortrait(), back);
                }
            }
        });
    }

    public void getFriends(String token, final Back back) {
        Map<String, String> params = new HashMap<>();
        params.put("TeamTribeToken", token);
        HttpUtil.post(URLStore.RELATION_ID_GETALL, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.d("获取好友", str);
                JsonIds json = new Gson().fromJson(str, JsonIds.class);
                if (json.getCode() != 0) {
                    back.failure(HttpUtil.getCodeMeg(json.getCode()));
                } else {
                    Bundle bundle = new Bundle();
                    ArrayList<Integer> f = json.getRelation();
                    if (f == null)
                        f = new ArrayList<>();
                    bundle.putIntegerArrayList("friends", f);
                    back.success(bundle);
                }
            }
        });
    }

    public void getMessages(String token, int type, final Back back) {
        Map<String, String> params = new HashMap<>();
        params.put("TeamTribeToken", token);
        params.put("type", type + "");
        HttpUtil.post(URLStore.MESSAGE_GET, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.d("获取消息", str);
                JsonMessage json = new Gson().fromJson(str, JsonMessage.class);
                if (json.getCode() != 0) {
                    back.failure(HttpUtil.getCodeMeg(json.getCode()));
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putParcelableArrayList("messages", json.getMessage());
                    back.success(bundle);
                }
            }
        });
    }

    public void getRequest(String token, int id, final Back back) {
        Map<String, String> params = new HashMap<>();
        params.put("TeamTribeToken", token);
        params.put("id", id + "");
        HttpUtil.post(URLStore.RELATION_REQUEST_GET, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.d("获取加好友信息", str);
                JsonRequest json = new Gson().fromJson(str, JsonRequest.class);
                if (json.getCode() != 0) {
                    back.failure(HttpUtil.getCodeMeg(json.getCode()));
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putParcelable("request", json.getRelationRequest());
                    back.success(bundle);
                }
            }
        });
    }
}
